<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>QeePHP 应用开发权威指南</title>
<link href="css/base.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id="page">


<div class="guide-section">

  <div class="guide-header">
    <span class="nav">
      <a href="http://qee13.com/app/?action=docs">文档索引</a>
      &raquo;
      <a href="index.html">QeePHP 应用开发权威指南</a>
      &raquo;
      <a href="node-model.html">模型与 ORM</a>
      &raquo;
      查询 Read    </span>
  </div>

  <div class="guide-section-details formatted">
    
<h1>查询 Read</h1>

<p>QeePHP 的 ORM
提供了丰富的查询方法，可以满足各种需求。</p>

<h2>简单的查询</h2>

<p>每一个模型都提供了 find()
静态方法。这个方法本质上并不查询数据，而是返回一个
QDB_Select
对象。利用这个设计，我们可以采用连贯接口写出漂亮的查询代码：</p>

<pre class="php code"><span
class="co1">// 查询指定 ID 的 Post 对象</span>
<span
class="re0">$post</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="st_h">'id = ?'</span><span
class="sy0">,</span> <span class="re0">$id</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getOne</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span
class="co1">// 查询所有符合条件的 Post 对象</span>
<span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="st_h">'level_ix &gt; ?'</span><span
class="sy0">,</span> <span class="re0">$level_ix</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<h2>指定查询条件</h2>

<p>从上面的代码可以看出，我们在 find()
方法中直接指定查询条件，然后用 getOne() 或 getAll()
来返回结果。</p>

<p>find() 方法的参数和 QDB_Select::where()
方法相同，支持下列参数格式：</p>

<pre class="php code">find<span class="br0">&#40;</span>查询条件<span
class="sy0">,</span> 查询参数<span class="nu0">1</span><span
class="sy0">,</span> 查询参数<span class="nu0">2</span><span
class="sy0">,</span> <span class="sy0">...</span><span
class="br0">&#41;</span></pre>

<p>查询条件通常使用字符串形式。字符串中的问号（?）将被视为<strong>参数占位符</strong>。按照占位符出现的顺序，提供给
find() 方法的后续参数将被填入查询条件中。例如：</p>

<pre class="php code"><span class="re0">$id</span> <span
class="sy0">=</span> <span class="nu0">1</span>
find<span
class="br0">&#40;</span><span class="st_h">'id = ?'</span><span
class="sy0">,</span> <span class="re0">$id</span><span
class="br0">&#41;</span>
<span class="co1">// 等同于</span>
find<span
class="br0">&#40;</span><span class="st_h">'id = 1'</span><span
class="br0">&#41;</span>
&nbsp;
&nbsp;
<span class="re0">$level_ix</span> <span
class="sy0">=</span> <span class="nu0">3</span>
<span
class="re0">$confirmed</span> <span class="sy0">=</span> <span
class="kw2">true</span>
find<span class="br0">&#40;</span><span
class="st_h">'level_ix &gt; ? AND confirmed = ?'</span><span
class="sy0">,</span> <span class="re0">$level_ix</span><span
class="sy0">,</span> <span class="re0">$confirmed</span><span
class="br0">&#41;</span>
<span class="co1">// 等同于</span>
find<span
class="br0">&#40;</span><span
class="st_h">'level_ix &gt; 3 AND confirmed = true'</span><span
class="br0">&#41;</span></pre>

<p>由于 QeePHP
会对替换占位符的参数值进行转义，因此使用参数占位符可以避免出现
SQL 注入漏洞。</p>

<p> </p>

<p>除了使用字符串做查询条件，QeePHP
还支持使用数组做查询条件。例如：</p>

<pre class="php code"><span class="re0">$cond</span> <span
class="sy0">=</span> <span class="kw3">array</span><span
class="br0">&#40;</span><span class="st_h">'id'</span> <span
class="sy0">=&gt;</span> <span class="nu0">3</span><span
class="sy0">,</span> <span class="st_h">'level_ix'</span> <span
class="sy0">=&gt;</span> <span class="nu0">1</span><span
class="br0">&#41;</span>
find<span class="br0">&#40;</span><span
class="re0">$cond</span><span class="br0">&#41;</span>
<span
class="co1">// 等同于</span>
find<span class="br0">&#40;</span><span
class="st_h">'id = 3 AND level_ix = 1'</span><span
class="br0">&#41;</span></pre>

<p>使用数组时，数组的键名将被视为查询条件中的字段名，而键值则是查询参数。多个字段和参数会用
AND
操作进行连接。同样，参数值也会进行自动转义，确保安全。比起使用字符串，数组做为查询条件缺乏灵活性。但是个别情况下会更方便一点，开发者可以酌情选择。</p>

<h3>使用命名参数</h3>

<p>前面提到了用问号（?）来做参数占位符，但这要求后续的查询参数必须按照顺序提供。如果使用命名参数，则没有这个限制：</p>

<pre class="php code">find<span class="br0">&#40;</span><span
class="st_h">'id = :id OR level_ix &gt; :leve_ix'</span><span
class="sy0">,</span> <span class="kw3">array</span><span
class="br0">&#40;</span><span class="st_h">'id'</span> <span
class="sy0">=&gt;</span> <span class="re0">$id</span><span
class="sy0">,</span> <span class="st_h">'level_ix'</span> <span
class="sy0">=&gt;</span> <span class="nu0">3</span><span
class="br0">&#41;</span><span class="br0">&#41;</span></pre>

<p>查询条件仍然是字符串，并使用以冒号（:）开头的单词来标识一个参数占位符。find()
方法的后续参数则以数组形式提供，数组键名与参数占位符同名即可。</p>

<h3>使用 QDB_Select 组合多个查询条件</h3>

<p>如果查询条件非常复杂，而我们又不方便使用字符串做查询条件，那么可以通过
find() 方法返回的 QDB_Select 对象来组装复杂查询，或者使用
QDB_Cond 对象来封装复杂的查询条件。</p>

<p>QDB_Select 对象提供了 where()、orWhere()
方法，可以让我们通过连贯接口添加多个查询条件：</p>

<pre class="php code">Post<span class="sy0">::</span><span
class="me2">find</span><span class="br0">&#40;</span><span
class="st_h">'level_ix &gt; ?'</span><span class="sy0">,</span> <span
class="re0">$level_ix</span><span class="br0">&#41;</span>
    <span
class="sy0">-&gt;</span><span class="me1">where</span><span
class="br0">&#40;</span><span class="st_h">'confirm = ?'</span><span
class="sy0">,</span> <span class="kw2">true</span><span
class="br0">&#41;</span>
    <span class="sy0">-&gt;</span><span
class="me1">where</span><span class="br0">&#40;</span><span
class="st_h">'author_id = ?'</span><span class="sy0">,</span> <span
class="re0">$author_id</span><span class="br0">&#41;</span>
    <span
class="sy0">-&gt;</span><span class="me1">getAll</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span></pre>

<p>或者将 find() 方法返回的 QDB_Select 对象保存起来：</p>

<pre class="php code"><span class="re0">$select</span> <span
class="sy0">=</span> Post<span class="sy0">::</span><span
class="me2">find</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
<span
class="re0">$select</span><span class="sy0">-&gt;</span><span
class="me1">where</span><span class="br0">&#40;</span><span
class="sy0">...</span><span class="br0">&#41;</span><span
class="sy0">;</span>
<span class="re0">$select</span><span
class="sy0">-&gt;</span><span class="me1">where</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span
class="re0">$posts</span> <span class="sy0">=</span> <span
class="re0">$select</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<h3>使用 QDB_Cond 构造复杂查询条件</h3>

<p>如果需要动态构造查询条件，使用 QDB_Cond
有时是个更好的选择。</p>

<p>每一个 QDB_Cond 对象封装一组查询条件。由于 QDB_Cond
是可以无限嵌套的，所以理论上可以构造任意复杂度的查询条件。</p>

<pre class="sql code">user_id <span class="sy0">&gt;</span> <span
class="nu0">1000</span> <span class="kw1">AND</span> is_valid <span
class="sy0">=</span> <span class="nu0">1</span></pre>

<p>使用 QDB_Cond 构造上述查询条件很简单：</p>

<pre class="php code"><span class="re0">$cond</span> <span
class="sy0">=</span> <span class="kw2">new</span> QDB_Cond<span
class="br0">&#40;</span><span class="st_h">'user_id &gt; ?'</span><span
class="sy0">,</span> <span class="re0">$user_id</span><span
class="br0">&#41;</span><span class="sy0">;</span>
<span
class="re0">$cond</span><span class="sy0">-&gt;</span><span
class="me1">andCond</span><span class="br0">&#40;</span><span
class="st_h">'is_valid = ?'</span><span class="sy0">,</span> <span
class="kw2">true</span><span class="br0">&#41;</span><span
class="sy0">;</span></pre>

<p>当然也可以使用连贯接口形式：</p>

<pre class="php code"><span class="re0">$cond</span> <span
class="sy0">=</span> QDB_Cond<span class="sy0">::</span><span
class="me2">create</span><span class="br0">&#40;</span><span
class="st_h">'user_id &gt; ?'</span><span class="sy0">,</span> <span
class="re0">$user_id</span><span class="br0">&#41;</span>
    <span
class="sy0">-&gt;</span><span class="me1">andCond</span><span
class="br0">&#40;</span><span class="st_h">'is_valid = ?'</span><span
class="sy0">,</span> <span class="kw2">true</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<p>还可以构造嵌套的查询条件：</p>

<pre class="php code"><span class="re0">$cond</span> <span
class="sy0">=</span> QDB_Cond<span class="sy0">::</span><span
class="me2">create</span><span class="br0">&#40;</span><span
class="st_h">'user_id &gt; 200'</span><span class="br0">&#41;</span>
    <span
class="sy0">-&gt;</span><span class="me1">orCond</span><span
class="br0">&#40;</span>QDB_Cond<span class="sy0">::</span><span
class="me2">create</span><span class="br0">&#40;</span><span
class="st_h">'level_ix = 3 AND is_valid = 1'</span><span
class="br0">&#41;</span><span class="br0">&#41;</span><span
class="sy0">;</span>
<span
class="co1">// 等同于 user_id &gt; 200 OR (level_ix = 3 AND is_valid = 1)</span></pre>

<p>最后将 QDB_Cond 对象传递给 find() 或 where()
等方法就行了：</p>

<pre class="php code">Post<span class="sy0">::</span><span
class="me2">find</span><span class="br0">&#40;</span><span
class="re0">$cond</span><span class="br0">&#41;</span><span
class="sy0">-&gt;</span><span class="me1">getAll</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span></pre>

<h2>限定查询结果</h2>

<p>要限定查询结果的数量，可以有几种方法：</p>

<pre class="php code"><span
class="co1">// 执行查询，并限定最多返回 10 个结果</span>
<span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">get</span><span class="br0">&#40;</span><span
class="nu0">10</span><span class="br0">&#41;</span><span
class="sy0">;</span>
<span
class="co1">// 同等效果的其他方式</span>
<span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">top</span><span class="br0">&#40;</span><span
class="nu0">10</span><span class="br0">&#41;</span><span
class="sy0">-&gt;</span><span class="me1">getAll</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span>
<span class="re0">$posts</span> <span
class="sy0">=</span> Post<span class="sy0">::</span><span
class="me2">find</span><span class="br0">&#40;</span><span
class="sy0">...</span><span class="br0">&#41;</span><span
class="sy0">-&gt;</span><span class="me1">limit</span><span
class="br0">&#40;</span><span class="nu0">0</span><span
class="sy0">,</span> <span class="nu0">10</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
<span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">limitPage</span><span class="br0">&#40;</span><span
class="nu0">1</span><span class="sy0">,</span> <span class="nu0">10</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span
class="co1">// 执行查询，从第 21 个符合条件的结果开始，返回最多 10 个结果</span>
<span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">limit</span><span class="br0">&#40;</span><span
class="nu0">20</span><span class="sy0">,</span> <span class="nu0">10</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
<span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">limitPage</span><span class="br0">&#40;</span><span
class="nu0">3</span><span class="sy0">,</span> <span class="nu0">10</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<p>如果希望通过变量指定查询结果数，那么应该使用 top()
等方法。不过一定要注意 limit() 的第一个参数是以 0
为基数的，也就是说假如要从第 21 个结果开始返回，那么
limit() 方法的第一个参数应该是 20。</p>

<p>limitPage()
是专门为分页查询提供的方法，第一个参数是页码，以 1
为基数。第二个参数则是每页的大小。如果有必要，还可以通过第三个参数指定页码的基数（默认为
1）。</p>

<p>如果我们明确只需要返回一个结果，那么使用 getOne()
方法即可：</p>

<pre class="php code"><span class="re0">$post</span> <span
class="sy0">=</span> Post<span class="sy0">::</span><span
class="me2">find</span><span class="br0">&#40;</span><span
class="sy0">...</span><span class="br0">&#41;</span><span
class="sy0">-&gt;</span><span class="me1">getOne</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span>
<span class="co1">// 或者，不建议使用</span>
<span
class="re0">$post</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">one</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">get</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<h2>分页查询</h2>

<p>分页查询是常见的操作，QeePHP
提供了一系列的方法来简化这项工作。</p>

<p>首先，limitPage()
方法可以按照页码、每页大小的方式来限定查询结果数量和起始位置。而
getPagination() 和 fetchPagination()
方法则用于获得计算好的分页信息，以便在用户界面中显示出合适的分页导航连接。</p>

<p>要实现分页，需要控制器和视图配合完成。控制器中确定分页的参数，而视图则显示分页导航连接：</p>

<pre class="php code"><span class="co1">// 控制器</span>
<span
class="kw2">class</span> Controller_Posts <span
class="kw2">extends</span> Controller_Abstract
<span
class="br0">&#123;</span>
    <span
class="co4">/**
     * 分页显示
     */</span>
    <span
class="kw2">function</span> actionIndex<span class="br0">&#40;</span><span
class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span
class="co1">// 确定页码</span>
        <span class="re0">$page</span> <span
class="sy0">=</span> <span class="kw3">intval</span><span
class="br0">&#40;</span>request<span class="br0">&#40;</span><span
class="st_h">'page'</span><span class="sy0">,</span><span
class="nu0">1</span><span class="br0">&#41;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span
class="co1">// 每页 30 个结果</span>
        <span
class="re0">$page_size</span> <span class="sy0">=</span> <span
class="nu0">30</span><span class="sy0">;</span>
&nbsp;
        <span
class="co1">// 进行分页查询，并取得分页信息</span>
        <span
class="re0">$pagination</span> <span class="sy0">=</span> <span
class="kw2">null</span><span class="sy0">;</span>
        <span
class="re0">$posts</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">limitPage</span><span class="br0">&#40;</span><span
class="re0">$page</span><span class="sy0">,</span> <span
class="re0">$page_size</span><span
class="br0">&#41;</span>
                                <span
class="sy0">-&gt;</span><span class="me1">fetchPagination</span><span
class="br0">&#40;</span><span class="re0">$pagination</span><span
class="br0">&#41;</span>
                                <span
class="sy0">-&gt;</span><span class="me1">get</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span>
&nbsp;
        <span
class="co1">// 将查询结果和分页数据传递到视图</span>
        <span
class="re0">$this</span><span class="sy0">-&gt;</span>_view<span
class="br0">&#91;</span><span class="st_h">'posts'</span><span
class="br0">&#93;</span> <span class="sy0">=</span> <span
class="re0">$posts</span><span class="sy0">;</span>
        <span
class="re0">$this</span><span class="sy0">-&gt;</span>_view<span
class="br0">&#91;</span><span class="st_h">'pagination'</span><span
class="br0">&#93;</span> <span class="sy0">=</span> <span
class="re0">$pagination</span><span class="sy0">;</span>
    <span
class="br0">&#125;</span>
<span class="br0">&#125;</span></pre>

<blockquote>
	<p>请参考 _code/control/pagination.php 控件</p>
</blockquote>

<pre class="php code"><span class="co1">// 视图文件</span>
<span
class="re0">$this</span><span class="sy0">-&gt;</span>_control<span
class="br0">&#40;</span><span class="st_h">'pagination'</span><span
class="sy0">,</span> <span class="st_h">'my-pagination'</span><span
class="sy0">,</span> <span class="kw3">array</span><span
class="br0">&#40;</span>
    <span class="st_h">'pagination'</span> <span
class="sy0">=&gt;</span> <span class="re0">$pagination</span><span
class="sy0">,</span>
<span class="br0">&#41;</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<p>上面控制器的代码使用了 fetchPagination()
方法。该方法会把分页信息存入传入的参数中（$pagination），而
getPagination()
方法则是通过返回值来获得分页信息。可以根据需要选择使用，例如：</p>

<pre class="php code"><span
class="co1">// 进行分页查询，并取得分页信息</span>
<span
class="re0">$select</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">limitPage</span><span class="br0">&#40;</span><span
class="re0">$page</span><span class="sy0">,</span> <span
class="re0">$page_size</span><span class="br0">&#41;</span><span
class="sy0">;</span>
&nbsp;
<span
class="co1">// 将查询结果和分页数据传递到视图</span>
<span
class="re0">$this</span><span class="sy0">-&gt;</span>_view<span
class="br0">&#91;</span><span class="st_h">'pagination'</span><span
class="br0">&#93;</span> <span class="sy0">=</span> <span
class="re0">$select</span><span class="sy0">-&gt;</span><span
class="me1">getPagination</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
<span
class="re0">$this</span><span class="sy0">-&gt;</span>_view<span
class="br0">&#91;</span><span class="st_h">'posts'</span><span
class="br0">&#93;</span>      <span class="sy0">=</span> <span
class="re0">$select</span><span class="sy0">-&gt;</span><span
class="me1">getAll</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span></pre>

<h2>聚合查询</h2>

<p>聚合查询是指取的符合条件的记录总数、某个字段的统计值（合计、最大、最小、平均）等操作。</p>

<pre class="php code"><span
class="co1">// 取得符合条件的记录总数</span>
<span
class="re0">$count</span> <span class="sy0">=</span> Post<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getCount</span><span class="br0">&#40;</span><span
class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span
class="co1">// 取得符合条件的订单的金额合计</span>
<span
class="re0">$sum</span> <span class="sy0">=</span> Order<span
class="sy0">::</span><span class="me2">find</span><span
class="br0">&#40;</span><span class="sy0">...</span><span
class="br0">&#41;</span><span class="sy0">-&gt;</span><span
class="me1">getSum</span><span class="br0">&#40;</span><span
class="st_h">'order_price'</span><span class="br0">&#41;</span><span
class="sy0">;</span></pre>

<p>与 getCount()、getSum() 类似，还可以使用
getAvg()、getMax()、getMin() 等方法。</p>

<p>如果希望在一次查询中获得多个聚合查询结果，应该使用略微不同的代码：</p>

<pre class="php code"><span class="re0">$arr</span> <span
class="sy0">=</span> Order<span class="sy0">::</span><span
class="me2">find</span><span class="br0">&#40;</span><span
class="sy0">...</span><span class="br0">&#41;</span><span
class="sy0">-&gt;</span><span class="me1">count</span><span
class="br0">&#40;</span><span class="st_h">'*'</span><span
class="sy0">,</span> <span class="st_h">'orders_count'</span><span
class="br0">&#41;</span>
                       <span
class="sy0">-&gt;</span><span class="me1">sum</span><span
class="br0">&#40;</span><span class="st_h">'order_price'</span><span
class="sy0">,</span> <span class="st_h">'orders_price_sum'</span><span
class="br0">&#41;</span>
                       <span
class="sy0">-&gt;</span><span class="me1">max</span><span
class="br0">&#40;</span><span class="st_h">'order_price'</span><span
class="sy0">,</span> <span class="st_h">'max_order_price'</span><span
class="br0">&#41;</span>
                       <span
class="sy0">-&gt;</span><span class="me1">min</span><span
class="br0">&#40;</span><span class="st_h">'order_pirce'</span><span
class="sy0">,</span> <span class="st_h">'min_order_price'</span><span
class="br0">&#41;</span><span class="sy0">;</span>
                       <span
class="sy0">-&gt;</span><span class="me1">asArray</span><span
class="br0">&#40;</span><span
class="br0">&#41;</span>
                       <span
class="sy0">-&gt;</span><span class="me1">get</span><span
class="br0">&#40;</span><span class="br0">&#41;</span><span
class="sy0">;</span>
&nbsp;
dump<span class="br0">&#40;</span><span
class="re0">$arr</span><span class="br0">&#41;</span><span
class="sy0">;</span>
<span class="co1">// 输出结果为：</span>
<span
class="co1">// array(</span>
<span
class="co1">//   'orders_count'     =&gt; 记录总数</span>
<span
class="co1">//   'orders_price_sum' =&gt; 金额合计</span>
<span
class="co1">//   'max_order_price'  =&gt; 最大订单金额</span>
<span
class="co1">//   'min_order_price'  =&gt; 最小订单金额</span>
<span
class="co1">// )</span></pre>

<h2>更多查询选项</h2>

<p>因为模型的 find() 方法实际返回一个 QDB_Select 对象，所以
QDB_Select 对象的所有方法都可以使用。详情请参考 QDB_Select
的文档（开发指南 &gt; 数据库 &gt; 查询对象）。</p>

<p>此外，QeePHP
还支持许多与模型关联有关的查询选项，详情请参考（开发指南
&gt; 模型 &gt; 关联）。</p>

<p> </p>

<p> </p>

<p> </p>
$Id: model-query.texy 2009-03-10 07:48:18Z Tim13 $
  </div>

  <div class="guide-footer">

    <table border="0" width="100%">
      <tr>
        <td align="left" width="200">
                    &laquo;
          <a href="node-model-protection.html">保护模型</a>
          
        </td>

        <td align="center">
          本章：<a href="node-model.html">模型与 ORM</a>
          <br />
          <a href="index.html">返回索引页</a>
        </td>

        <td align="right" width="200">
                    <a href="node-model-create.html">创建 Create</a> 
          &raquo;
                  </td>
      </tr>
    </table>

  </div>

</div>


</div>

</body>
</html>


